package com.lijiaxing.controller;

import com.lijiaxing.pojo.PermissionRes;
import com.lijiaxing.pojo.Result;
import com.lijiaxing.service.userService;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated //参数验证注解
@RestController //请求注解
@RequestMapping("/user")
public class userController {
    @Autowired
    private userService userservice;

    //注册
    @PostMapping("/authentication")
    public Result authentication(@RequestParam("userName") @Pattern(regexp = "^1[0-9]{10}$") String utel, @RequestParam("passWord") @Pattern(regexp = "^[0-9a-zA-Z._]{6,}") String upwd, @RequestParam("validCode") @Pattern(regexp = "^[0-9]{6}$") String ucode) {
        return userservice.setRegister(utel, upwd, ucode);
    }

    @GetMapping("/getPermissionList")
    public Result<String> getpermissionlist() {
        return Result.success("{\"data\":[{\"id\":1,\"label\":\"控制台\"},{\"id\":2,\"label\":\"权限管理\"," +
                "\"disabled\":true,\"children\":[{\"id\":4,\"label\":\"账号管理\",\"disabled\":true}," +
                "{\"id\":5,\"label\":\"菜单管理\",\"disabled\":true}]},{\"id\":3,\"label\":\"陪诊管理\",\"children\":[" +
                "{\"id\":6,\"label\":\"陪护管理\"},{\"id\":7,\"label\":\"订单管理\"}]}]}", "OK");
    }

    @PostMapping("/addPermission")
    public Result<Object> addPermission(@RequestParam("pname") String pname, @RequestParam("permissions") Integer[] permission) {
        PermissionRes.Permission p = new PermissionRes.Permission(-1, pname, permission);
        return userservice.addPermission(p);
    }

    @GetMapping("/getTenPermissions")
    public Result<PermissionRes> getPermissions(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        return userservice.queryTenPermission(pageNum, pageSize);
    }

    @GetMapping("/getAllPermissions")
    public Result<PermissionRes> getAllPermissions() {
        return userservice.queryAllPermission();
    }

    @PostMapping("/updatePermission")
    public Result<Object> updatePermission(@RequestParam("pid") Integer pid, @RequestParam("pname") String pname, @RequestParam("permissions") Integer[] permission) {
        PermissionRes.Permission p = new PermissionRes.Permission(pid, pname, permission);
        return userservice.updatePermission(p);
    }

}
